
String.prototype.trim = function() {
    return this.replace(/(\s*$)|(^\s*)/g, '');
}

function $I(id) {
    return document.getElementById(id);
}

function $N(name) {
    return document.getElementsByName(name);
}

var net = new Object();

//请求状态属性
net.READY_STATE_UNINITIALIZED = 0;    //未初始化
net.READY_STATE_LOADING = 1;          //正在加载
net.READY_STATE_LOADED = 2;           //已加载
net.READY_STATE_INTERACTIVE = 3;      //交互中
net.READY_STATE_COMPLETE = 4;         //完成

net.ContentLoader = function(url, params, method, onload, onerror) {
    this.req = null;
    this.onload = onload;
    this.onerror = (onerror) ? onerror : this.defaultError;
    this.loadXMLDoc(url, method, params);
}

net.ContentLoader.prototype.loadXMLDoc = function(url, method, params) {
    if (!method) {
        method = "GET";
    }
    if (method == "POST") {
        contentType = 'application/x-www-form-urlencoded;charset=gb2312';
    }
    if (window.XMLHttpRequest) {
        this.req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        try {
            this.req = new ActiveXObject('MSXML2.XMLHTTP');
        }
        catch (e) {
            try {
                this.req = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e) {
                this.onerror.call(this);
            }
        }
    }
    if (this.req) {
        try {
            var loader = this;
            this.req.onreadystatechange = function() {
                loader.onReadyState.call(loader);
            }
            this.req.open(method, url, true);
            if (contentType) {
                this.req.setRequestHeader('Content-Type', contentType);
            }
            this.req.send(params);
        }
        catch (err) {
            this.onerror.call(this);
        }
    }
}


net.ContentLoader.prototype.onReadyState = function() {
    var req = this.req;
    if (req.readyState == net.READY_STATE_COMPLETE) {
        if (req.status == 200 || req.status == 0) {
            this.onload.call(this, req.responseText);
        }
        else {
            this.onerror.call(this, req.responseText);
        }
    }
}

net.ContentLoader.prototype.defaultError = function() {
    alert("error fetching data!"
    + "\n\nreadyState:" + this.req.readyState
    + "\nstatus: " + this.req.status
    + "\nheaders: " + this.req.getAllResponseHeaders());
}


var loading = "正在验证...";
var servererr = "对不起，服务器忙，请您稍候再试。";
var msg = new Array();
msg[0] = "<img style='display:inline;margin-bottom:-3px;padding:0 10px; height:15px' src='images/reg_ok.gif' />"
	msg[1] = "会员帐号可由英文、中文、数字、下划线组成，长度2-20个字符。";
	msg[2] = "此项为必填项，请输入会员帐号。";
	msg[3] = "会员帐号格式错误，请重新填写。";
	msg[4] = "此会员帐号已被注册，请重新填写。";

	msg[5] = "请输入6-20个英文字母、数字组成的字符。";
	msg[6] = "此项为必填项，请设置您的密码。";
	msg[7] = "格式错误，请输入6-20个字母、数字组成的字符。";
	msg[8] = "再输入一遍上面所填的密码";
	msg[9] = "此项为必填项，请再次输入您的密码。";
	msg[10] = "两次密码输入不一致，请重新填写。";

	msg[11] = "用于密码找回，并可定期收到我社推荐书目。";
	msg[12] = "邮箱注册后不可修改，用于取回密码使用";
	msg[13] = "Email格式错误，请重新填写。";
	msg[14] = "此Email已被注册，请重新填写。";

	msg[15] = "用于密码找回，例如：\"我宠物的名字\"。";
	msg[16] = "此项为必填项，请输入您的密码提示。";

	msg[17] = "用于密码找回，例如：\"旺财\"。";
	msg[18] = "此项为必填项，请输入您的提示答案。";

	msg[19] = "抱歉，您必须同意吃了吗服务条款后，才能注册。";

	msg[20] = "此项为必填项，请输入验证码。";
	msg[21] = "验证码错误，请重新输入。";
	msg[22] = "验证码已过期，请重新输入。"
	msg[23] = "为了防止恶意注册，请输入验证码。";

var userid = false;
var pwd = false;
var again = false;
var email = false;
var question = false;
var answer = false;
var code = false;
var nike = false;
var chekcbox = false;
var phone = false;

function checkfrom() {

	 
	    email_check();
	    nikename_check(); 
	    pwd_check(); 
	    checkbox_check();
	    again_check(); 
        
	    if (pwd && again && email && chekcbox) {
	    	showloadx("", "", "dd");
	        return true;
	    }
	    
	    
	    return false;
}

function ShowMSg(obj, msg, iserr) {
    obj.innerHTML = msg;
    if (iserr == true) {
        obj.className = "notice_error";
    }
    else {
        obj.className = "notice";
    }
    obj.style.display = "inline";
}

function userid_focus() {
    ShowMSg($I('msgUserID'), msg[1], false);
}

function userid_check() {
    var val = $I('tbUserID').value;
    var err = $I('msgUserID');

    if (val.trim() == "") {
        ShowMSg(err, msg[2], true);
        userid = false;
        return false;
    }
    if (!/^(\w|[\u4e00-\u9fa5]){2,20}$/.test(val)) {
        ShowMSg(err, msg[3], true);
        userid = false;
        return false;
    }

    ShowMSg(err, loading, false);
    new net.ContentLoader("Ajax/AjaxCheck.aspx", "type=customername&value=" + val, "POST", userid_deal, userid_err);
}

function userid_deal() {
    if (this.req.responseText == "1") {
        ShowMSg($I('msgUserID'), msg[0], false);
        userid = true;
    }
    else {
        ShowMSg($I('msgUserID'), msg[4], true);
        userid = false;
    }
}

function userid_err() {
    ShowMSg($I('msgUserID'), servererr, true);
    userid = false;
}

function pwd_focus() {
    ShowMSg($I('msgPwd'), msg[5], false);
}

function pwd_check() {
    var val = $I('TBpassword').value;
    var err = $I('msgPwd');
    if (val.trim() == "") {
        err.style.color = 'red'
        ShowMSg(err, msg[6], true);
        pwd = false;
        return;
    }
    if (!/^[a-zA-Z0-9]{6,20}$/.test(val)) {
        err.style.color = 'red'
        ShowMSg(err, msg[7], true);
        pwd = false;
        return;
    }
    if ($I('tbAgain').value != "") {
        again_check()
    }
    ShowMSg(err, msg[0], false);
    pwd = true;
    return;
}

function again_focus() {
    $I('msgAgain').style.color = "#4c4c4c";
    ShowMSg($I('msgAgain'), msg[8], false);
}

function again_check() {
    var val = $I('tbAgain').value;
    var err = $I('msgAgain');

    if (val.trim() == "") {
        $I('msgAgain').style.color = 'red'
        ShowMSg(err, msg[9], true);
        again = false;
        return false;
    }
    if (val != $I('TBpassword').value) {
        $I('msgAgain').style.color = 'red'
        ShowMSg(err, msg[10], true);
        again = false;
        return false;
    }
    $I('msgAgain').style.color = "#4c4c4c";
    ShowMSg(err, msg[0], false);
    again = true;
    return true;
}

function email_focus() {
    document.getElementById('emailmsg').style.color = "#4c4c4c";
    ShowMSg($I('emailmsg'), msg[12], false);
}

function email_check() {
    var val = document.getElementById('tbEmail').value;
    var err = document.getElementById('emailmsg');

    if (val.trim() == "") {
        err.style.color = "red";
        ShowMSg(err, msg[12], true);
        email = false;
        return;
    }
    if (!/^[.\-_a-zA-Z0-9]+@[\-_a-zA-Z0-9]+\.[a-zA-Z0-9]/.test(val)) {
        err.style.color = "red";
        ShowMSg(err, msg[13], true);
        email = false;
        return;
    }
    email = true;
    ShowMSg(err, msg[0], false);
    return true;
}

function email_deal() {
    if (this.req.responseText == "1") {
        ShowMSg($I('emailmsg'), msg[0], false);
        email = true;
    }
    else {
        ShowMSg($I('emailmsg'), msg[14], true);
        $I('emailmsg').style.color = 'red'
        email = false;
    }
}

function email_err() {
    ShowMSg($I('emailmsg'), servererr, true);
}

function question_focus() {
    ShowMSg($I('msgQuestion'), msg[15], false);
}

function question_check() {
    if ($I('tbQuestion').value.trim() == "") {
        ShowMSg($I('msgQuestion'), msg[16], true);
        question = false;
        return false;
    }
    ShowMSg($I('msgQuestion'), msg[0], false);
    question = true;
    return true;
}

function answer_focus() {
    ShowMSg($I('msgAnswer'), msg[17], false);
    alert("17");
}

function answer_check() {
    if ($I('tbAnswer').value.trim() == "") {
        ShowMSg($I('msgAnswer'), msg[18], true);
        answer = false;
    }
    else {
        ShowMSg($I('msgAnswer'), msg[0], false);
        answer = true;
    }
}

function code_focus() {
    $I('msgCode').style.color = "#4c4c4c";
    ShowMSg($I('msgCode'), msg[23], false);
}

function code_check() {
    if ($I('tbCode').value.trim() == "") {
        $I('msgCode').style.color = 'red'
        ShowMSg($I('msgCode'), msg[20], true);
        code = false;
    }
    else {
        $I('msgCode').style.color = "#4c4c4c";
        ShowMSg($I('msgCode'), loading, false);
        new net.ContentLoader("Ajax/AjaxCheck.aspx", "type=code&value=" + $I('tbCode').value, "POST", code_deal, code_err);
    }
}

function code_deal() {
    if (this.req.responseText == "1") {
        ShowMSg($I('msgCode'), msg[0], false);
        code = true;
    }
    else {
        $I('msgCode').style.color = 'red'
        ShowMSg($I('msgCode'), msg[21], true);
        code = false;
    }
}

function code_err() {
    ShowMSg($I('msgCode'), servererr, true);
}

function detail_check() {
    if ($I("trDetail").style.display == "none")
        $I("trDetail").style.display = "block";
    else
        $I("trDetail").style.display = "none";
}

function checkbox_check() {
    if ($I("Jchagree").checked == false) {
        document.getElementById('checkboxmsg').style.color = "red";
        ShowMSg($I('checkboxmsg'), msg[19], false);
        $I("checkboxmsg").style.display = "";
        chekcbox = false;
    }
    else {
        chekcbox = true;
        $I("checkboxmsg").style.display = "none";
    }
}

function nikename_focus() {
    document.getElementById('nikenamemsg').style.color = "#4c4c4c";
    ShowMSg($I('nikenamemsg'), "昵称注册后不可修改，可以用手机号码注册", false);
}

function nikename_check() {
    if ($I('tbnikename').value == "") {
        $I('nikenamemsg').style.color = 'red'
        nike = false;
    }
    else {
        
        var url = "user.do?m=checkname";
    	
    	$("#nikenamemsg").load( url,{name:$("#tbnikename").val()},function(responseText,textStauts,XMLHttpRequest){
    		if(responseText == "success"){
    			ShowMSg($I('nikenamemsg'), msg[0], false);
    		}
    		else{
    			$I('nikenamemsg').style.color = 'red';
	    		$("#nikenamemsg").html(responseText);
    		}
    	})
    }

}

function nike_deal() {
    if (this.req.responseText == "1") {
        ShowMSg($I('nikenamemsg'), msg[0], false);
        nike = true;
    }
    else {
        $I('nikenamemsg').style.color = 'red'
        	ShowMSg($I('nikenamemsg'), '此昵称已经被注册了,你重新填写.', true);
        nike = false;
    }
}

function nike_err() {
    ShowMSg($I('nikenamemsg'), servererr, true);
}


function phone_check() {
    var myphone = $I('tbmobilephone').value;
    if (myphone == "") {
        //        alert('�������ֻ�');
        //        phone = false;
        phone = true;
        return;
    }
    var model = /^\d{11}$/;
    if (!model.test(myphone)) {
    	alert('手机格式错误。');
        phone = false;
        return;
    }
    //    jQuery.ajax(
    //    {
    //        type: "post",
    //        url: "Ajax/AjaxCheck.aspx",
    //        async: false,
    //        data: "type=phone&value=" + myphone,
    //        success: function(msg) {
    //            if (msg == "0") {
    //                alert("���ֻ��Ѿ���ע���ˣ����������롣");
    //                phone = false;
    //                return;
    //            }
    //            phone = true;
    //        }
    //    })
    phone = true;
}